package com.qqs.socket;

import com.qqs.eq.Equipment;
import com.qqs.eq.EquipmentManager;
import com.qqs.util.ConvertUtil;

import io.netty.channel.Channel;

public final class Controller {
	private static Channel getChannel(String chipid) {
		String ip = EquipmentManager.eqm.getEqChipToipMap().get(chipid);
		return ChannelManager.channelIpMap.get(ip).channel();
	}

	private static Channel getChannelByIp(String ip) {
		return ChannelManager.channelIpMap.get(ip).channel();
	}

	/**
	 * ctrlLight控制回路状态
	 * 
	 * @param chipid
	 *            MAC地址例如0199BC0D
	 * @param channel
	 *            回路取值1-4
	 * @param onff
	 *            控制字，on 或 off
	 */
	public static void ctrlLight(String chipid, int node, int channel, String onff) {
		if (chipid == null || channel < 0 || channel > 4 || onff == null)
			return;
		Channel c = getChannel(chipid);
		char[] chipids = ConvertUtil.string2Char2(chipid);
		char nodeChannel = (char) (((node << 4) | (int) Math.pow(2, channel - 1)) & 0xff);
		if ("on".equals(onff)) {
			char[] cmd = Command.ctrlLight(chipids, nodeChannel, nodeChannel);
			c.write(cmd);
		} else if ("off".equals(onff)) {
			char[] cmd = Command.ctrlLight(chipids, nodeChannel, (char) 0x00);
			c.write(cmd);
		}
	}

	public static void ctrlLight(String id, String onff) {
		Equipment eq = EquipmentManager.eqm.getEqMap().get(id);
		Channel c = getChannelByIp(eq.getGwip());
		char[] chipids = ConvertUtil.string2Char2(eq.getEqchipid());
		char nodeChannel = (char) (((eq.getNode() << 4) | (int) Math.pow(2, eq.getChannel() - 1)) & 0xff);
		if ("on".equals(onff)) {
			char[] cmd = Command.ctrlLight(chipids, nodeChannel, nodeChannel);
			c.write(cmd);
		} else if ("off".equals(onff)) {
			char[] cmd = Command.ctrlLight(chipids, nodeChannel, (char) 0x00);
			c.write(cmd);
		}
	}

	/**
	 * queryLight异步查询回路状态
	 * 
	 * @param chipid
	 */
	public static void queryLight(String chipid) {
		if (chipid == null)
			return;
		Channel c = getChannel(chipid);

		char[] chipids = ConvertUtil.string2Char2(chipid);
		char[] cmd = Command.queryLight(chipids);
		c.write(cmd);
	}

	public static void queryLightByEqId(String id) {
		Equipment eq = EquipmentManager.eqm.getEqMap().get(id);
		Channel c = getChannelByIp(eq.getGwip());
		char[] chipids = ConvertUtil.string2Char2(eq.getEqchipid());
		char[] cmd = Command.queryLight(chipids);
		c.write(cmd);
	}

	/**
	 * 
	 * queryElectric 异步查询电能
	 * 
	 * @param chipid
	 */
	public static void queryElectric(String chipid) {
		if (chipid == null)
			return;
		Channel c = getChannel(chipid);
		char[] chipids = ConvertUtil.string2Char2(chipid);
		char[] cmd = Command.queryElectric(chipids);
		c.write(cmd);
	}

	public static void queryElectricByEqId(String id) {
		Equipment eq = EquipmentManager.eqm.getEqMap().get(id);
		Channel c = getChannelByIp(eq.getGwip());
		char[] chipids = ConvertUtil.string2Char2(eq.getEqchipid());
		char[] cmd = Command.queryElectric(chipids);
		c.write(cmd);
	}

	/**
	 * 
	 * queryLed 异步查询led
	 * 
	 * @param chipid
	 */
	public static void queryLed(String chipid) {
		if (chipid == null)
			return;
		Channel c = getChannel(chipid);

		char[] chipids = ConvertUtil.string2Char2(chipid);
		char[] cmd = Command.queryLed(chipids);
		c.write(cmd);
	}

	public static void queryLedByEqId(String id) {
		Equipment eq = EquipmentManager.eqm.getEqMap().get(id);
		Channel c = getChannelByIp(eq.getGwip());
		char[] chipids = ConvertUtil.string2Char2(eq.getEqchipid());
		char[] cmd = Command.queryLed(chipids);
		c.write(cmd);
	}

	/**
	 * ctrlTemp 控制温控器
	 * 
	 * @param chipid
	 * @param onoff
	 * @param tempe
	 * @param wind
	 * @param mode
	 */
	public static void ctrlTemp(String chipid, String onoff, int tempe, String wind, String mode) {
		if (chipid == null)
			return;
		Channel c = getChannel(chipid);

		char[] chipids = ConvertUtil.string2Char2(chipid);
		char on = 0x00;
		if (onoff.equals("on")) {
			on = 0xFF;
		}
		// 温度
		int temperarture = tempe * 10;
		String strtmp = String.format("%1$04x", temperarture);
		char[] hex = new char[2];
		String[] temp = new String[2];
		for (int i = 0; i < 2; i++) {
			temp[i] = "0x" + strtmp.substring(2 * i, 2 * i + 2);
			hex[i] = ConvertUtil.hex2Char(temp[i])[0];
		}
		// wind
		char windch = 0xEE;
		if (wind != null) {
			if (wind.equals("auto")) {
				windch = 0x00;
			} else if (wind.equals("low")) {
				windch = 0x01;
			} else if (wind.equals("mid")) {
				windch = 0x02;
			} else if (wind.equals("high")) {
				windch = 0x03;
			}
		}
		// mode
		char modech = 0xEE;
		if (mode != null) {
			if (mode.equals("auto")) {
				modech = 0x00;
			} else if (mode.equals("cold")) {
				modech = 0x01;
			} else if (mode.equals("hot")) {
				modech = 0x02;
			} else if (mode.equals("wind")) {
				modech = 0x03;
			}
		}
		char[] cmd = Command.ctrlTemp(chipids, on, hex[0], hex[1], windch, modech);
		c.write(cmd);
	}

	public static void ctrlTempByEqid(String id, String onoff, int tempe, String wind, String mode) {
		Equipment eq = EquipmentManager.eqm.getEqMap().get(id);
		Channel c = getChannelByIp(eq.getGwip());
		char[] chipids = ConvertUtil.string2Char2(eq.getEqchipid());
		char on = 0x00;
		if (onoff.equals("on")) {
			on = 0xFF;
		}
		// 温度
		int temperarture = tempe * 10;
		String strtmp = String.format("%1$04x", temperarture);
		char[] hex = new char[2];
		String[] temp = new String[2];
		for (int i = 0; i < 2; i++) {
			temp[i] = "0x" + strtmp.substring(2 * i, 2 * i + 2);
			hex[i] = ConvertUtil.hex2Char(temp[i])[0];
		}
		// wind
		char windch = 0xEE;
		if (wind != null) {
			if (wind.equals("auto")) {
				windch = 0x00;
			} else if (wind.equals("low")) {
				windch = 0x01;
			} else if (wind.equals("mid")) {
				windch = 0x02;
			} else if (wind.equals("high")) {
				windch = 0x03;
			}
		}
		// mode
		char modech = 0xEE;
		if (mode != null) {
			if (mode.equals("auto")) {
				modech = 0x00;
			} else if (mode.equals("cold")) {
				modech = 0x01;
			} else if (mode.equals("hot")) {
				modech = 0x02;
			} else if (mode.equals("wind")) {
				modech = 0x03;
			}
		}
		char[] cmd = Command.ctrlTemp(chipids, on, hex[0], hex[1], windch, modech);
		c.write(cmd);
	}

	/**
	 * 
	 * ctrlCurtain 控制窗帘
	 * 
	 * @param chipid
	 * @param channel
	 * @param operate
	 */
	public static void ctrlCurtain(String chipid, int channel, String operate) {
		if (chipid == null)
			return;
		Channel c = getChannel(chipid);

		char[] chipids = ConvertUtil.string2Char2(chipid);
		char channelch = 0x01;
		if (channel == 2) {
			channelch = 0x02;
		}
		char status = 0x0F;
		if (operate.equals("close")) {
			status = 0x00;
		} else if (operate.equals("pause")) {
			status = 0x01;
		}
		char[] cmd = Command.ctrlCurtain(chipids, channelch, status);
		c.write(cmd);
	}

	public static void ctrlCurtain(String id, String operate) {
		Equipment eq = EquipmentManager.eqm.getEqMap().get(id);
		Channel c = getChannelByIp(eq.getGwip());
		char[] chipids = ConvertUtil.string2Char2(eq.getEqchipid());
		char channelch = 0x01;
		if (eq.getChannel() == 2) {
			channelch = 0x02;
		}
		char status = 0x0F;
		if (operate.equals("close")) {
			status = 0x00;
		} else if (operate.equals("pause")) {
			status = 0x01;
		}
		char[] cmd = Command.ctrlCurtain(chipids, channelch, status);
		c.write(cmd);
	}

	/**
	 * 
	 * ctrlLed 控制led
	 * 
	 * @param chipid
	 * @param R
	 * @param G
	 * @param B
	 */
	public static void ctrlLed(String chipid, int R, int G, int B) {
		if (chipid == null)
			return;
		Channel c = getChannel(chipid);

		char[] chipids = ConvertUtil.string2Char2(chipid);
		char[] cmd = Command.ctrlLed(chipids, R, G, B);
		c.write(cmd);
	}

	public static void ctrlLedByEqId(String id, int R, int G, int B) {
		Equipment eq = EquipmentManager.eqm.getEqMap().get(id);
		Channel c = getChannelByIp(eq.getGwip());
		char[] chipids = ConvertUtil.string2Char2(eq.getEqchipid());
		char[] cmd = Command.ctrlLed(chipids, R, G, B);
		c.write(cmd);
	}

	/**
	 * 
	 * ctrlDoor 控制门锁
	 * 
	 * @param chipid
	 * @param operate
	 */
	public static void ctrlDoor(String chipid, String operate) {
		if (chipid == null)
			return;
		Channel c = getChannel(chipid);

		char[] chipids = ConvertUtil.string2Char2(chipid);
		if (operate.equals("open")) {
			char[] cmd = Command.ctrlDoor(chipids, (char) 0x05);
			c.write(cmd);
		}

	}

	public static void ctrlDoorByEqId(String id, String operate) {
		Equipment eq = EquipmentManager.eqm.getEqMap().get(id);
		Channel c = getChannelByIp(eq.getGwip());
		char[] chipids = ConvertUtil.string2Char2(eq.getEqchipid());
		if (operate.equals("open")) {
			char[] cmd = Command.ctrlDoor(chipids, (char) 0x05);
			c.write(cmd);
		}

	}

	/**
	 * 
	 * ctrlMusic 控制背景音乐
	 * 
	 * @param musicid
	 * @param ctrlstr
	 */
	public static void ctrlMusic(String chipid, String ctrlstr) {
		if (chipid == null)
			return;
		Channel c = getChannel(chipid);

		char[] chipids = ConvertUtil.string2Char2(chipid);
		char[] cmd = Command.ctrlMusic(chipids, ctrlstr);
		c.write(cmd);
	}

	public static void ctrlMusicByEqId(String id, String ctrlstr) {
		Equipment eq = EquipmentManager.eqm.getEqMap().get(id);
		Channel c = getChannelByIp(eq.getGwip());
		char[] chipids = ConvertUtil.string2Char2(eq.getEqchipid());
		char[] cmd = Command.ctrlMusic(chipids, ctrlstr);
		c.write(cmd);
	}

	/**
	 * 
	 * learnOn 灯光控制器开关学习／清除
	 * 
	 * @param chipid
	 * @param onchipid
	 * @param operate
	 * @param channel
	 * @param onmode
	 */
	public static void learnOn(String chipid, String onchipid, String operate, int channel, String onmode) {
		if (chipid == null)
			return;
		Channel c = getChannel(chipid);

		char[] chipids = ConvertUtil.string2Char2(chipid);
		char[] onchipids = ConvertUtil.string2Char2(onchipid);
		char operatech = 0;
		if (operate.equals("learn")) {
			operatech = 0x32;
		} else if (operate.equals("clear")) {
			operatech = 0x33;
		}
		char channelch = 0;
		if (channel == 1) {
			channelch = 0x00;
		} else if (channel == 2) {
			channelch = 0x01;
		} else if (channel == 3) {
			channelch = 0x02;
		} else if (channel == 4) {
			channelch = 0x03;
		}
		char directionChar = 0, mode = 0;
		if (onmode.equals("左开关")) {
			directionChar = 0x50;
			mode = 0xFF;
		} else if (onmode.equals("左上取反")) {
			directionChar = 0x50;
			mode = 0x00;
		} else if (onmode.equals("左下取反")) {
			directionChar = 0x70;
			mode = 0x00;
		} else if (onmode.equals("右开关")) {
			directionChar = 0x10;
			mode = 0xFF;
		} else if (onmode.equals("右上取反")) {
			directionChar = 0x10;
			mode = 0x00;
		} else if (onmode.equals("右下取反")) {
			directionChar = 0x30;
			mode = 0x00;
		} else if (onmode.equals("上开关")) {
			directionChar = 0x15;
			mode = 0xFF;
		} else if (onmode.equals("上取反")) {
			directionChar = 0x15;
			mode = 0x00;
		} else if (onmode.equals("下取反")) {
			directionChar = 0x37;
			mode = 0x00;
		}
		char[] cmd = Command.learnOn(chipids, onchipids, operatech, channelch, directionChar, mode);
		c.write(cmd);
	}

	/**
	 * 
	 * learnBody 灯光控制器人体传感器学习／清除
	 * 
	 * @param chipid
	 * @param bodychipid
	 * @param operate
	 * @param channel
	 */
	public static void learnBody(String chipid, String bodychipid, String operate, int channel) {
		if (chipid == null)
			return;
		Channel c = getChannel(chipid);

		char[] chipids = ConvertUtil.string2Char2(chipid);
		char[] bodychipids = ConvertUtil.string2Char2(bodychipid);
		char operatech = 0;
		if (operate.equals("learn")) {
			operatech = 0x36;
		} else if (operate.equals("clear")) {
			operatech = 0x37;
		}
		char channelch = 0;
		if (channel == 1) {
			channelch = 0x00;
		} else if (channel == 2) {
			channelch = 0x01;
		} else if (channel == 3) {
			channelch = 0x02;
		} else if (channel == 4) {
			channelch = 0x03;
		}
		char[] cmd = Command.learnBody(chipids, bodychipids, operatech, channelch);
		c.write(cmd);
	}

	/**
	 * 
	 * setIllumination 设置照度
	 * 
	 * @param chipid
	 * @param illumination
	 */
	public static void setIllumination(String chipid, int illumination) {
		if (chipid == null)
			return;
		Channel c = getChannel(chipid);

		char[] chipids = ConvertUtil.string2Char2(chipid);
		char illu = 0;
		if (illumination >= 0 && illumination <= 1000) {
			illu = (char) (illumination / 4);
		}
		char[] cmd = Command.setIllumination(chipids, illu);
		c.write(cmd);
	}

	/**
	 * 
	 * setDelayTime 设置延迟关灯时间
	 * 
	 * @param chipid
	 * @param time
	 */
	public static void setDelayTime(String chipid, int time) {
		if (chipid == null)
			return;
		Channel c = getChannel(chipid);

		char[] chipids = ConvertUtil.string2Char2(chipid);
		String delayStr = ConvertUtil.int2hex(time).replaceAll(" ", "");
		char p1, p2;
		if (delayStr.length() == 2) {
			p1 = 0x00;
			p2 = ConvertUtil.hex2Char("0x" + delayStr)[0];
		} else {
			char[] hext = new char[2];
			String[] tempt = new String[2];
			for (int i = 0; i < 2; i++) {
				tempt[i] = "0x" + delayStr.substring(2 * i, 2 * i + 2);
				hext[i] = ConvertUtil.hex2Char(tempt[i])[0];
			}
			p1 = hext[0];
			p2 = hext[1];
		}
		char[] cmd = Command.setDelayTime(chipids, p1, p2);
		c.write(cmd);
	}

	/**
	 * 
	 * setPriorityLevel 设置开关的优先级别
	 * 
	 * @param chipid
	 * @param level
	 *            0 开关和传感器同级，1 开关优先
	 */
	public static void setPriorityLevel(String chipid, int level) {
		if (chipid == null)
			return;
		Channel c = getChannel(chipid);
		char[] chipids = ConvertUtil.string2Char2(chipid);
		char l = 0;
		if (level == 0) {
			l = 0x00;
		} else if (level == 1) {
			l = 0xFF;
		}
		char[] cmd = Command.setPriorityLevel(chipids, l);
		c.write(cmd);
	}

	/**
	 * 
	 * setPriorityTime 设置开关的优先时间
	 * 
	 * @param chipid
	 * @param time
	 */
	public static void setPriorityTime(String chipid, int time) {
		if (chipid == null)
			return;
		Channel c = getChannel(chipid);
		char[] chipids = ConvertUtil.string2Char2(chipid);
		String pStr = ConvertUtil.int2hex(time).replaceAll(" ", "");
		char p1, p2;
		if (pStr.length() == 2) {
			p1 = 0x00;
			p2 = ConvertUtil.hex2Char("0x" + pStr)[0];
		} else {
			char[] hext = new char[2];
			String[] tempt = new String[2];
			for (int i = 0; i < 2; i++) {
				tempt[i] = "0x" + pStr.substring(2 * i, 2 * i + 2);
				hext[i] = ConvertUtil.hex2Char(tempt[i])[0];
			}
			p1 = hext[0];
			p2 = hext[1];
		}
		char[] cmd = Command.setPriorityTime(chipids, p1, p2);
		c.write(cmd);
	}
}
